using System;

namespace csammisrun.OscarLib.Utility
{
	/// <summary>
	/// Provides static methods for processing messages in SNAC family 0x0009 -- privacy management service
	/// </summary>
	internal static class SNAC09
	{
    /// <summary>
		/// Sends a request for parameter information -- SNAC(09,02)
		/// </summary>
		/// <param name="sess">A <see cref="Session"/> object</param>
		public static void RequestParametersList(Session sess)
		{
			SNACHeader sh = new SNACHeader();
			sh.FamilyServiceID = (ushort)SNACFamily.PrivacyManagementService;
			sh.FamilySubtypeID = (ushort)PrivacyManagementService.ServiceParametersRequest;
			sh.Flags = 0x0000;
			sh.RequestID = sess.GetNextRequestID();

			DataPacket dp = Marshal.BuildDataPacket(sess, sh, null);
			SNACFunctions.BuildFLAP(dp);
		}

		/// <summary>
		/// Processes the parameter information sent by the server -- SNAC(09,03)
		/// </summary>
		/// <param name="dp">A <see cref="DataPacket"/> object with a buffer containing SNAC(09,03)</param>
		public static void ProcessParametersList(DataPacket dp)
		{
			byte[] SNACData = dp.Data;

			int index = 0;
			ushort max_visiblelist_size = 0;
			ushort max_invisiblelist_size = 0;

			while(index < SNACData.Length)
			{
				ushort key = Marshal.ByteArrayToUshort(SNACData, ref index);
				ushort keylength = Marshal.ByteArrayToUshort(SNACData, ref index);
				switch(key)
				{
					case 0x0001:	// Max number of visible list entries
						max_visiblelist_size = Marshal.ByteArrayToUshort(SNACData, ref index);
						break;
					case 0x0002:	// Max number of invisible list entries
						max_invisiblelist_size = Marshal.ByteArrayToUshort(SNACData, ref index);
						break;
					default:
						index += keylength;
						break;
				}
			}

			dp.ParentSession.ParameterSetArrived();
		}
	}
}
